package com.cruise.system.userhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.cruise.system.userapi.dto.UpdateUserDataCountDTO;
import com.cruise.system.userapi.entity.SysRoleUser;
import com.cruise.system.userapi.entity.SysUser;
import com.cruise.system.userapi.vo.GetUserInfoVO;
import com.cruise.system.userhandle.mapper.SysRoleUserMapper;
import com.cruise.system.userhandle.mapper.SysUserMapper;
import com.cruise.system.userhandle.service.SysUserService;
import com.cruise.system.userhandle.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭闯
 * @since 2021-01-13
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public SysUser findByUsername(String username) {
        return this.baseMapper.selectOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
    }

    @Override
    public SysUser findByPhone(String phone) {
        return this.baseMapper.selectOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getPhone, phone));
    }


    @Override
    public String regist(SysUser sysUser) {
        sysUser.setNickname(StrUtil.getStringRandom(5));
        sysUserMapper.insert(sysUser);
        String roleId = "17";
        //默认的角色是 普通用户 ，
        sysRoleUserMapper.insert(new SysRoleUser(sysUser.getId(), roleId, new Date()));
        return sysUser.getId();

    }

    @Override
    public void updateSysUser(SysUser sysUser) {

        sysUser.setUpdateTime(new Date());
        sysUserMapper.updateById(sysUser);
    }

    /**
     *  查询手机号是否存在
     * @param phoneNumber
     * @return
     */
    @Override
    public Boolean checkPhoneAvailable(String phoneNumber) {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getPhone, phoneNumber)
                .eq(SysUser::getStatus, "0")
                .eq(SysUser::getDelFlag, "0"));
        if(sysUser == null){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 我的首页用户信息
     * @param userId
     * @return
     */
    @Override
    public GetUserInfoVO getUserInfo(String userId) {

        GetUserInfoVO getUserInfoVO = new GetUserInfoVO();
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getId, userId));
        BeanUtils.copyProperties(sysUser, getUserInfoVO);
        return getUserInfoVO;
    }

    @Override
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS)
    public void updateUserDataCount(UpdateUserDataCountDTO updateUserDataCountDTO) {
        if(updateUserDataCountDTO.getIsAdd()){
            sysUserMapper.updateUserDataCountAdd(updateUserDataCountDTO);
        }else{
            sysUserMapper.updateUserDataCountMinus(updateUserDataCountDTO);
        }
    }
}
